package com.edu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.eduservice.entity.EduCourse;
import com.edu.eduservice.entity.EduCourseDescription;
import com.edu.eduservice.entity.EduTeacher;
import com.edu.eduservice.entity.EduVideo;
import com.edu.eduservice.mapper.EduCourseMapper;
import com.edu.eduservice.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.eduservice.vo.*;
import com.edu.eduservicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-05-30
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    //课程描述
    @Autowired
private EduCourseDescriptionService courseDescriptionService;
    //课程
    @Autowired
    private EduCourseService eduCourseService;
    //课程老师
    @Autowired
    private EduTeacherService eduTeacherService;
    //课时
    @Autowired
    private EduVideoService videoService;
    //章节
    @Autowired
    private EduChapterService chapterService;


    /**
     * 查询前8个热门课程
     * @return
     */
    @Override
    @Cacheable(value = "banner",key = "'findHotCourse'")
    public List<EduCourse> findHotCourse(){
        QueryWrapper<EduCourse> wrapper =new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        List<EduCourse> eduList = baseMapper.selectList(wrapper);
        return eduList;
    }





    /**
     * 新增课程
     * @param courseInfoVo
     * @return
     */
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //保存课程基本信息
        EduCourse eduCourse = new EduCourse();
        eduCourse.setStatus(EduCourse.COURSE_DRAFT);
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.insert(eduCourse);
        if (i==0){
            throw new GuliException(20001,"课程信息保存失败!");
        }

        //保存课程详情信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(eduCourse.getId());

        boolean save1 = courseDescriptionService.save(eduCourseDescription);
        if (!save1){
            throw new GuliException(20001,"课程详情信息保存失败");
        }

        return eduCourse.getId();
    }


    /**
     * 查询课程Vo
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoVo getCoureseInfo(String courseId) {

        //1.查询课程表
        EduCourse course = eduCourseService.getById(courseId);
        CourseInfoVo courseInfoVo =new CourseInfoVo();
        BeanUtils.copyProperties(course,courseInfoVo);

        //2.查询描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());


        return courseInfoVo;
    }

    /**
     * 修改课程
     * @param courseInfoVo
     */
    @Override
    public void updateCoureseInfo(CourseInfoVo courseInfoVo) {

        /**
         * 修改课程信息
         */
        EduCourse course =new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,course);
        int i = baseMapper.updateById(course);
        if (i==0){
            throw  new GuliException(20001,"修改课程信息失败!");
        }


        /**
         * 修改课程描述信息
         */
        EduCourseDescription courseDescription =new EduCourseDescription();
        courseDescription.setId(courseInfoVo.getId());
        courseDescription.setDescription(courseInfoVo.getDescription());
        boolean b = courseDescriptionService.updateById(courseDescription);
        if (!b){
            throw  new GuliException(20001,"修改课程描述失败!");
        }

    }

    /**
     * 根据id查询 课程Vo
     * @param courseId
     * @return
     */

    @Override
    public CoursePublishVo publicCourseInfo(String courseId) {
       return baseMapper.getPublicCoureseInfo(courseId);

    }



    /**
     * 课程分页条件查询
     * @param pageParam
     * @param courseQuery
     */
    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper wrapper = new QueryWrapper();
        QueryWrapper wrapper1 = new QueryWrapper();
        QueryWrapper wrapper2 = new QueryWrapper();
        if (courseQuery==null){
            baseMapper.selectPage(pageParam,wrapper);
            return;
        }
        String title = courseQuery.getTitle();
        String teacherName = courseQuery.getTeacherName();
        BigDecimal price = courseQuery.getPrice();
        String description = courseQuery.getDescription();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();

        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }

        if (!StringUtils.isEmpty(teacherName)){
            wrapper1.like("name",teacherName);
        }

        if (!StringUtils.isEmpty(price)){
            wrapper.like("price",price);
        }

        if (!StringUtils.isEmpty(description)){
            wrapper2.like("description",description);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.like("begin",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.like("end",end);
        }

        baseMapper.selectPage(pageParam,wrapper);
        courseDescriptionService.getOne(wrapper2);
        eduTeacherService.getOne(wrapper1);

    }

    /**
     * 删除课程
     * @param courseId
     */
    @Override
    public void removeCourse(String courseId) {
        //删除小节
        videoService.removeByCourseId(courseId);

        //删除章节
        chapterService.removeByCourseId(courseId);

        //删除描述
        courseDescriptionService.removeById(courseId);

        //删除本身
        int count = baseMapper.deleteById(courseId);
        if (count==0){
            throw  new GuliException(20001,"删除失败!");
        }

    }

    /**
     * 根据老师id查询对应课程!
     * @param teacherId
     * @return
     */
    @Override
    public List<EduCourse> selectByTeacherId(String teacherId) {
        QueryWrapper<EduCourse> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("teacher_id",teacherId);
        //按照最后更新时间倒序排列
        queryWrapper.orderByDesc("gmt_modified");

        List<EduCourse> eduCourses = baseMapper.selectList(queryWrapper);
        return eduCourses;
    }


    /**
     * 课程列表显示!
     * @param pageParam
     * @param courseQuery
     * @return
     */
    @Override
    public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
        QueryWrapper<EduCourse> queryWrapper =new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())){
            queryWrapper.eq("subject_parent_id",courseQuery.getSubjectParentId());
        }

        if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
            queryWrapper.eq("subject_id", courseQuery.getSubjectId());
        }

        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {
            queryWrapper.orderByDesc("buy_count");
        }

        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam,queryWrapper);
        //
        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        HashMap<String, Object> map = new HashMap<>();

        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    /**
     * * 获取课程信息
     * @param id
     * @return
     */
    @Override
    public CourseWebVo selectInfoWebById(String id) {
        this.updatePageViewCount(id);

        return baseMapper.selectInfoWebById(id);
    }


    /**
     * * 获取课程信息
     * @param id
     */
    @Override
    public void updatePageViewCount(String id) {
        EduCourse course = baseMapper.selectById(id);
        course.setViewCount(course.getViewCount()+1);
        baseMapper.updateById(course);

    }


}
